package jp.sinmetal.trainer.action;

import java.util.List;

import javax.annotation.Resource;

import jp.sinmetal.trainer.entity.Member;
import jp.sinmetal.trainer.form.MemberForm;
import jp.sinmetal.trainer.service.MemberService;

import org.seasar.framework.beans.util.Beans;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;

/**
 * メンバーアクション
 * @author sin
 *
 */
public class MemberAction extends AbstractAction {


    /**
     * メンバー一覧
     * @author sin
     */
    public List<Member> memberItems;

    /**
     * メンバーアクションフォーム
     * @author sin
     */
    @ActionForm
    @Resource
    protected MemberForm memberForm;

    /**
     * メンバーサービス
     * @author sin
     */
    @Resource
    protected MemberService memberService;

//    /**
//     * エントリーポイント
//     * メンバー一覧
//     * @author sin
//     *
//     * @return
//     */
//    @Execute(validator = false)
//    public String index() {
//        memberItems = memberService.findAll();
//
//        return "list.jsp";
//    }

    /**
     * マイページ
     * @author sin
     *
     * @return
     */
    @Execute(validator = false)
    public String show() {
    	if (!memberInfo.isLogin()) {
    		return "/authentication/";
    	}

        Member entity = memberService.findById(memberInfo.memberId);
        Beans.copy(entity, memberForm).dateConverter(DATE_FORMAT).execute();
        return "show.jsp";
    }

    /**
     * 編集
     * @return
     */
    @Execute(validator = false)
    public String edit() {
        Member entity = memberService.findById(memberInfo.memberId);
        Beans.copy(entity, memberForm).dateConverter(DATE_FORMAT).execute();
        return "edit.jsp";
    }

    /**
     * メンバー新規登録
     * @author sin
     *
     * @return
     */
    @Execute(validator = false)
    public String create() {
        return "create.jsp";
    }

//    @Execute(validator = false, urlPattern = "delete/{memberId}/{version}", redirect = true)
//    public String delete() {
//        Member entity = Member.getInstance(memberForm);
//        memberService.delete(entity);
//        return "/member/";
//    }

    /**
     * メンバー作成サービス呼び出し
     * @author sin
     *
     * @return
     */
    @Execute(input = "create.jsp", redirect = true)
    public String insert() {
        Member entity = Member.getInstance(memberForm);
        memberService.insertMember(entity);
        Beans.copy(entity, memberInfo).execute();
        return "/member/show/";
    }

    /**
     * メンバー編集サービス呼び出し
     * @author sin
     *
     * @return
     */
    @Execute(input = "edit.jsp", redirect = true)
    public String update() {
        Member entity = Member.getInstance(memberForm);
        entity.memberId = memberInfo.memberId;
        memberService.updateMember(entity);
        return "/member/show";
    }
}